const http = require('http');
const url = require('url');
 
// 创建HTTP服务器
const server = http.createServer((req, res) => {
    // 解析请求
    const parsedUrl = url.parse(req.url, true);
 
    // 定义接口路由
    const path = parsedUrl.pathname;
    const query = parsedUrl.query;
 
    if (path === '/cars') {
        console.log('cars接口被调用了。。。');
        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify([
        {
          id: '001',
          brand: '奔驰',
        },
        {
          id: '002',
          brand: '宝马',
        },
        {
          id: '003',
          brand: '奥迪',
        },
        {
          id: '004',
          brand: '问界',
        }]));
    } else {
        res.writeHead(404, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({ message: 'Not Found' }));
    }
});
 
// 监听5001端口
server.listen(5001, () => {
    console.log('服务器运行在 http://localhost:5001/');
});